package com.onenine.app.listener;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.SparseIntArray;

import com.onenine.common.util.ApUtils;

/**
 * 创建时间：2018/8/21 0021 14:11
 * 创建人：fly
 * 功能描述：通知消息的计数管理
 */
public class NotificationCountReceiver extends BroadcastReceiver {

    public static final String ACTION_CLEAR_NOTIFICATION = "com.onenine.action.NOTIFICATION_CLEAR";
    private static SparseIntArray idCountArrays = new SparseIntArray();

    @Override
    public void onReceive(Context context, Intent intent) {
        if (TextUtils.equals(intent.getAction(), ACTION_CLEAR_NOTIFICATION)) {
            notifyRemoveNotification(intent.getIntExtra("id", 0));
        }
    }

    /**
     * 通知加入notification
     *
     * @param notificationId
     */
    public static void notifyAddNotification(int notificationId) {
        int count = idCountArrays.get(notificationId, -1);
        if (count != -1) {
            idCountArrays.put(notificationId, ++count);
        } else {
            idCountArrays.put(notificationId, 1);
        }
    }

    /**
     * 通知移除notification
     *
     * @param uuid
     */
    public static void notifyRemoveNotification(String uuid) {
        idCountArrays.delete(uuid.hashCode());
    }

    /**
     * 通知移除notification
     *
     * @param notificationId
     */
    public static void notifyRemoveNotification(int notificationId) {
        idCountArrays.delete(notificationId);
    }

    /**
     * 通知移除全部
     */
    public static void notifyRemoveAllNotification() {
        idCountArrays.clear();
    }

    /**
     * 获取创建广播的意图
     *
     * @param context
     * @param uuid
     * @return
     */
    public static Intent getNotificationClearIntent(Context context, int uuid) {
        Intent intent = new Intent(context, NotificationCountReceiver.class);
        intent.setPackage(context.getPackageName());
        intent.setAction(NotificationCountReceiver.ACTION_CLEAR_NOTIFICATION);
        intent.putExtra("id", uuid);
        return intent;
    }

    /**
     * 获取某个Notification的数量
     *
     * @param notificationId
     * @return 未处理数量
     */
    public static int getNotificationCountById(int notificationId) {
        return idCountArrays.get(notificationId, 0);
    }

    /**
     * 是否存在通知
     *
     * @return
     */
    public static boolean hasNotification() {
        return idCountArrays.size() > 0;
    }

    /**
     * 移除单条通知
     *
     * @param uuid
     */
    public static void cancelNotificationById(String uuid) {
        NotificationManager notificationMgr = (NotificationManager) ApUtils
                .getApp().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationMgr.cancel(uuid.hashCode());
        notifyRemoveNotification(uuid);
    }

    /**
     * 移除所有通知消息
     */
    public static void cancelAllNotification() {
        NotificationManager notificationMgr = (NotificationManager) ApUtils
                .getApp().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationMgr.cancelAll();
        notifyRemoveAllNotification();
    }

}
